package esdemo;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.junit.Before;
import org.junit.Test;

public class AddAggregationApi {
	private TransportClient client;

	@Before
	public void init() throws UnknownHostException {
		client = TransportClient.builder().build()
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
	}
	
	@Test
	public void subAgg(){
		SearchResponse sr = client.prepareSearch()
			    .addAggregation(
			        AggregationBuilders.terms("by_country").field("country")
			        .subAggregation(AggregationBuilders.dateHistogram("by_year")
			            .field("dateOfBirth")
			            .interval((DateHistogramInterval.YEAR))
			            .subAggregation(AggregationBuilders.avg("avg_children").field("children"))
			        )
			    )
			    .execute().actionGet();
	}
	
	
	////////////////// Metrics aggregations (可度量的聚合)  min 、max 、avg  /////////////////////
	
	
}
